import java.sql.*;
import java.util.*;

/**
 * @author 支绍昆
 * 2024/3/20
 * 数据库查询
 **/
public class Database {

    public Map<String, String> queryList(String id) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/data?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false",
                    "root",
                    "123456");
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT id,script FROM view WHERE script IS NOT NULL and id > '" + id + "' order by id");
            Map<String, String> result = new LinkedHashMap<>();
            while (resultSet.next()) {
                result.put(resultSet.getString(1), resultSet.getString(2));
            }

            resultSet.close();
            statement.close();
            conn.close();

            return result;
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public String queryOne(String id) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/data?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false",
                    "root",
                    "123456");
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT script FROM view WHERE id = '" + id + "'");
            String result = "";
            while (resultSet.next()) {
                result = resultSet.getString(1);
            }

            resultSet.close();
            statement.close();
            conn.close();
            return result;
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
